#!/bin/bash
function yum_repair() {
go_yumOpt=
echo -e "${blue}当前正在执行的操作为:${nocolor} ${green}修复CentOS6 YUM源${nocolor}"
echo -e "${red}注意：此选项只适用于CentOS 6${nocolor}"
echo -ne "
此选项将会执行以下操作:
--------------------
${green}1.${nocolor}备份现有BASE和EPEL仓库
${green}2.${nocolor}启用新的YUM源
-------------------
"
while [[ ! $go_yumOpt == [YyNn] ]]
do
    echo -ne "继续[Yy,Enter],退出[Nn]: "
    read  go_yumOpt
    go_yumOpt=${go_yumOpt:-Y}
    echo  ""
done

if [[ $go_yumOpt != [Yy] ]];then
   return 1
fi


echo -ne "${blue}选择Base仓库源提供者${nocolor}
1.阿里云
2.清华大学
请选择[1]："
read base_yum
if [[ $base_yum != [12] ]];then
    base_yum=1
fi
if [ "$base_yum" == "1" ];then
    base_yum_name="阿里云"
else
    base_yum_name="清华大学"
fi
echo ""

echo -ne "${blue}选择EPEL仓库源提供者${nocolor}
1.官方(推荐)
2.重庆大学
请选择[1]："
read epel_yum
if [[ $epel_yum != [12] ]];then
    epel_yum=1
fi
if [ "$epel_yum" == "1" ];then
    epel_yum_name="官方"
else
    epel_yum_name="重庆大学"
fi
echo ""


echo -ne "${blue}YUM源修复信息汇总${nocolor}
---------------------------
BASE仓库源提供者：$base_yum_name
EPEL仓库源提供者：$epel_yum_name
---------------------------
继续安装[Yy,Enter]，退出安装[Nn]: "
read isYn
isYn=${isYn:-y}
if [[ $isYn == [Nn] ]];then
   return 1
fi
echo ""

echo -e "${cyan}开始修复YUM源${nocolor}"
echo ""
sleep 0.15

####备份
echo -e "${Info} ${purple}备份CentOS-Base和epel${nocolor}"
sleep 0.5
/bin/mv -f /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak >/dev/null 2>&1
/bin/mv -f /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.bak >/dev/null 2>&1

#添加新的配置
echo -e "$Info ${purple}启用新的BASE源${nocolor}" 
sleep 0.5

if [ "$base_yum" == "1" ];then
        cat >/etc/yum.repos.d/CentOS-Base.repo<<'EOF'
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#
 
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/centos/$releasever/os/$basearch/
        http://mirrors.aliyuncs.com/centos-vault/centos/$releasever/os/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos-vault/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
 
#released updates 
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/centos/$releasever/updates/$basearch/
        http://mirrors.aliyuncs.com/centos-vault/centos/$releasever/updates/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos-vault/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
 
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/centos/$releasever/extras/$basearch/
        http://mirrors.aliyuncs.com/centos-vault/centos/$releasever/extras/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos-vault/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/centos/$releasever/centosplus/$basearch/
        http://mirrors.aliyuncs.com/centos-vault/centos/$releasever/centosplus/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos-vault/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
 
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/centos/$releasever/contrib/$basearch/
        http://mirrors.aliyuncs.com/centos-vault/centos/$releasever/contrib/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos-vault/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
EOF
else
    cat >/etc/yum.repos.d/CentOS-Base.repo<<'EOF'
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#released updates 
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib&infra=$infra
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
EOF
fi
    
echo -e "$Info ${purple}启用新的EPEL源${nocolor}" 
sleep 0.5
if [ "$epel_yum" == "1" ];then
        cat > /etc/yum.repos.d/epel.repo <<'EOF'
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=http://archives.fedoraproject.org/pub/archive/epel/6/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
 
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 6 - $basearch - Debug
baseurl=http://archives.fedoraproject.org/pub/archive/epel/6/$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=0
 
[epel-source]
name=Extra Packages for Enterprise Linux 6 - $basearch - Source
baseurl=http://archives.fedoraproject.org/pub/archive/epel/6/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=0
EOF
else
    cat > /etc/yum.repos.d/epel.repo <<'EOF'
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=https://mirrors.cqu.edu.cn/epel/6/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
 
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 6 - $basearch - Debug
baseurl=https://mirrors.cqu.edu.cn/epel/6/$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=0
 
[epel-source]
name=Extra Packages for Enterprise Linux 6 - $basearch - Source
baseurl=https://mirrors.cqu.edu.cn/epel/6/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=0
EOF
fi

sleep 1
echo ""
echo -ne "${cyan}YUM仓库修复完毕，按任意键返回!${nocolor}"
read -s -n1 goback
echo -e ""
}